﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace MedVis.Converters
{
    class Mm2PixelConverter : IValueConverter
    {
        #region IValueConverter Members
        const double factor = 96 / 25.4;
        double[] iv = MainWindow.getImageValues();
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double[] iv = MainWindow.getImageValues();
            double sf = 1;
            double mm = (double)value;
            double px = 0;
            double dpi = 0;
            double size = 0;
            if (parameter.Equals("W"))
            {
                sf = iv[0];
                dpi = iv[2];
                size = iv[4];
            }
            else
            {
                sf = iv[1];
                dpi = iv[3];
                size = iv[5];
            }
            px = (dpi / 25.4 * mm);
            if (parameter.Equals("OX"))
            {
                sf = iv[0];
                dpi = iv[2];
                size = iv[4];

                px = (mm * factor)*sf;
            }
            if (parameter.Equals("OY"))
            {
                sf = iv[1];
                dpi = iv[3];
                size = iv[5];

                px = (mm * factor) * sf;
            }

            Console.WriteLine(mm + " mm->px " + px);
            return px;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double[] iv = MainWindow.getImageValues();
            double sf = 1;
            double px = (double)value;
            double mm = 0;
            double dpi = 0;
            double size = 0;
            if (parameter.Equals("W"))
            {
                sf = iv[0];
                dpi = iv[2];
                size = iv[4];
            }
            else
            {
                sf = iv[1];
                dpi = iv[3];
                size = iv[5];
            }
            mm = (px * 25.4 / dpi);
            if (parameter.Equals("OX"))
            {
                sf = iv[0];
                dpi = iv[2];
                size = iv[4];

                mm = (px * factor);
            }
            if (parameter.Equals("OY"))
            {
                sf = iv[1];
                dpi = iv[3];
                size = iv[5];

                mm = (px * factor);
            }
            Console.WriteLine(px + " px->mm " + mm);
            return mm;
        }

        #endregion
    }
}
